import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/util/context_extension.dart';
import 'package:flutter_common/util/string_utils.dart';

import '../../flutter_common.dart';

/// name+末尾文字+箭头
class TextSelectItem extends StatelessWidget {
  final String name;
  final String? value;
  final String hint;
  final Function()? onTap;
  final bool arrow;
  ///文本是否可以复制
  final bool valueCopy;
  final Widget? arrowWidget;
  final Widget? nameWidget;

  final Color? backgroundColor;

  ///name字体颜色
  final Color? color;

  ///value字体颜色
  final Color? valueColor;

  ///hint字体颜色
  final Color? hintColor;
  final double? fontSize;
  final bool showLine;
  final bool showRed;

  ///[showLine]开启的时候，设置线左边的边距
  final ValueListenable<String>? controller;
  final CrossAxisAlignment crossAxisAlignment;

  ///通过修改padding 改变默认高度
  final EdgeInsetsGeometry namePadding;
  final EdgeInsetsGeometry valuePadding;
  final EdgeInsetsGeometry linePadding;

  final TextAlign textAlign;
  final TextStyle? contentStyle;

  const TextSelectItem({
    Key? key,
    this.name = "",
    this.value,
    this.hint = "请选择",
    this.onTap,
    this.fontSize,
    this.backgroundColor,
    this.arrow = true,
    this.arrowWidget,
    this.nameWidget,
    this.valueCopy = false,
    this.showRed = false,
    this.color = const Color(0xff333333),
    this.valueColor = const Color(0xff111111),
    this.hintColor = const Color(0xffBBBBBB),
    this.showLine = false,
    this.namePadding = const EdgeInsets.fromLTRB(15, 14, 10, 14),
    this.valuePadding = const EdgeInsets.fromLTRB(0, 14, 0, 14),
    this.linePadding = const EdgeInsets.fromLTRB(15, 0, 0, 0),
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.controller,
    this.contentStyle,
    this.textAlign = TextAlign.end,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (controller == null) return _buildBody(context, value);
    return ValueListenableBuilder<String>(
        valueListenable: controller!,
        builder: (context, value, child) => _buildBody(context, value));
  }

  Widget _buildBody(BuildContext context, String? value) {
    Widget content = Row(
      crossAxisAlignment: crossAxisAlignment,
      children: [
        Padding(
          padding: namePadding,
          child: nameWidget ?? Text.rich(TextSpan(
            children: [
              if (showRed)
                const TextSpan(text: "*", style: TextStyle(color: Colors.red)),
              TextSpan(text: name),
            ],
            style: TextStyle(fontSize: fontSize ?? fontSize28, color: color),
          )),
        ),
        Expanded(child: Padding(padding: valuePadding, child: _content(value))),
        if (arrow) arrowWidget ?? _icon(),
        SizedBox(width: arrow ? 10 : 15),
      ],
    );
    if (showLine) {
      content = Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          content,
          Padding(padding: linePadding, child: FxTheme().line)
        ],
      );
    }
    if (onTap != null) {
      content = Ink(
          width: double.infinity,
          color: backgroundColor,
          child: InkWell(onTap: () => throttle(onTap), child: content));
    } else if(backgroundColor != null){
      content = Container(color: backgroundColor, child: content);
    }
    return Material(color: Colors.transparent, child: content);
  }

  _icon() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: valuePadding.vertical / 2),
      child: const Icon(
        Icons.keyboard_arrow_right_outlined,
        size: 18,
        color: Color(0xFFCCCCCC),
      ),
    );
  }

  _content(String? value) {
    bool isHint = !value.isNullOrBlank;
    var str = isHint ? value! : hint;
    var style = TextStyle(
      fontSize: fontSize ?? fontSize28,
      color: isHint ? valueColor : hintColor,
      //最好比1.2小，因为TextAlign.end导致出现内容在底部，横向居中没对齐
      height: 1.2,
    );
    style = style.merge(contentStyle);
    if (valueCopy) {
      return SelectableText(str, textAlign: textAlign, style: style);
    }
    return Text(str, textAlign: textAlign, softWrap: true, style: style);
  }
}
